// 环境的切换
// const pubUrl = '';
// if (process.env.NODE_ENV === 'development') {
//   // 开发环境
//   pubUrl = '';
// } else if (process.env.NODE_ENV === 'debug') {
//   pubUrl = ''; // 调试环境
// } else if (process.env.NODE_ENV === 'production') {
//   pubUrl = ''; // 生产环境
// }
export type apiResult<T = any> = {
  cause: '';
  data: T;
  message: string;
  request: null;
  state: number;
  total: number;
  variables: any;
  version: null;
};
const http = <T = any>(options: any) => new Promise((resolve, reject) => {
  // eslint-disable-next-line no-undef
  uni.request({
    // 官方文档说是默认携带cookie（h5环境），实际体验并非如此
    withCredentials: true,
    url: options.url,
    method: options.method || 'get',
    data: options.data || {},

    // 请求成功的回调，可以在方法做返回状态码的判断。也可以直接返回resolve  ==》success: resolve，fail：reject
    success: (res: any) => {
      const { data } = res;
        data as apiResult<T>;
        if (data.state === 200) {
          resolve(data);
        }
        // 这里已经是接口异常了，不需要回调了
        // message.error(data?.message);
        throw new Error(data?.message);
    },
    // 请求失败的回调，可以在方法。反馈用户、提示用户，也可以直接返回reject  ==》success: resolve，fail：reject
    fail: (err) => {
      reject(err);
    },
  });
});
export const request = async <T = any>({
  method = 'get', url = '', data = {}, params = {},
}) => (await http({
  method,
  url,
  data,
  params,
})) as unknown as apiResult<T>;
